package com.lianxian.xindong.commonsdk.utils.filters;

import android.text.InputFilter;
import android.text.Spanned;

/**
 * @author EDZ
 */
public class EmojiFilter implements InputFilter {
    public static String BASE_REGEX;
    public static String REGEX_SING_LINE = "^[ 0-9a-zA-Z!#$%&'\"()*+,\\-.:;=?@\\[\\\\\\]^_`{|}~\\u4E00-\\u9FA5\\u3002\\uff1f\\uff01\\uff0c\\u3001\\uff1b\\uff1a\\u201c\\u201d\\u2018\\u2019\\uff08\\uff09\\u300a\\u300b\\u3008\\u3009\\u3010\\u3011\\u300e\\u300f\\u300c\\u300d\\ufe43\\ufe44\\u3014\\u3015\\u2026\\u2014\\uff5e\\ufe4f\\uffe5]+$";
    public static String REGEX_MULTI_LINE= "^[ \n0-9a-zA-Z!#$%&'\"()*+,\\-.:;=?@\\[\\\\\\]^_`{|}~\\u4E00-\\u9FA5\\u3002\\uff1f\\uff01\\uff0c\\u3001\\uff1b\\uff1a\\u201c\\u201d\\u2018\\u2019\\uff08\\uff09\\u300a\\u300b\\u3008\\u3009\\u3010\\u3011\\u300e\\u300f\\u300c\\u300d\\ufe43\\ufe44\\u3014\\u3015\\u2026\\u2014\\uff5e\\ufe4f\\uffe5]+$";

    private boolean isMultiLine;

    public EmojiFilter() {
        this(false);
    }

    public EmojiFilter(boolean isMultiLine) {
        this.isMultiLine = isMultiLine;
        BASE_REGEX = isMultiLine ? REGEX_MULTI_LINE : REGEX_SING_LINE;
    }

    public boolean isMultiLine() {
        return isMultiLine;
    }

    public void setMultiLine(boolean multiLine) {
        isMultiLine = multiLine;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
                               int dend) {
        if(source.toString().equals("")) {
            return null;
        }

        if(!source.toString().matches(BASE_REGEX)) {
            return "";
        } else {
            return source;
        }
    }
}
